/*
 * Class:PSideBar
 * 
 * Description: This class represent the sidebar for the calendar.
 * 
 * Version: 1.0 - Mohamed Ali Ayed - 26-11-2011
 * 		Initial implementation
 * 
 */
package calendarInterface2;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import calendarEventManager.CEMAgenda;
import calendarEventManager.CEMListColor;

import com.toedter.calendar.JCalendar;

import controller.CTLEngine;

@SuppressWarnings("serial")
public class PSideBar extends JPanel{
	JLabel lTitle;	
	JPanel pjCalendar;
	JPanel pAgenda;
	JCheckBox cbAgenda;
	JCalendar jCalendar_;
	private JPanel pListeAgenda;
	private JButton bUpdateAgenda;

	public PSideBar() {
		super();
		build();
	}
	
	public void build(){
		//set PROPERTIES's frame
		setPreferredSize(new Dimension(200,568)); 
		setVisible(true);
		setLayout(new BorderLayout());
		
		//add label title
		lTitle = new JLabel("Mini-Calendar & Agendas");
		lTitle.setOpaque(true);
		Font font = new Font("Arial",Font.ITALIC,16);
		lTitle.setFont(font);
		lTitle.setSize(new Dimension(200,68));
		add(lTitle,BorderLayout.NORTH);
		
		// add panel jCalendar
		pjCalendar = new JPanel(new BorderLayout());
		pjCalendar.setPreferredSize(new Dimension(200,200));
		jCalendar_ = new JCalendar();
		jCalendar_.setWeekOfYearVisible(false);
		jCalendar_.addPropertyChangeListener(new ActionCalendar());
		pjCalendar.add(jCalendar_,BorderLayout.CENTER);
		add(pjCalendar, BorderLayout.CENTER);
		
		// add panel pAgenda
		pAgenda = new JPanel();
		pAgenda.setLayout(new BorderLayout());
		bUpdateAgenda = new JButton("Update/Delete Agenda");
		bUpdateAgenda.addActionListener(new ActionListAgendaFrame());
		
		pAgenda.add(bUpdateAgenda,BorderLayout.NORTH);
		pListeAgenda = new JPanel();
		buildPAgenda(pListeAgenda);
		
		add(pAgenda,BorderLayout.SOUTH);
	}
	
	public void addAgendas(List<CEMAgenda> listAgenda){
		for (CEMAgenda oAgenda : listAgenda) {
			addJCheckBox(oAgenda);
		}
	}

	/**
	 * @param oAgenda
	 */
	public void addJCheckBox(CEMAgenda p_oAgenda) {
		CEMListColor liste = new CEMListColor();
		cbAgenda = new JCheckBox(p_oAgenda.getAgendaName());
		cbAgenda.setForeground(CalendarInterface.getColor(liste.get(p_oAgenda.getAgendaColour())));
		cbAgenda.addActionListener(new ActionSelectedAgenda(p_oAgenda));
		if(p_oAgenda.isActivated()) {
			cbAgenda.setSelected(true);
		}
		pListeAgenda.add(cbAgenda);
	}
	
	public void buildPAgenda(JPanel panel){
		panel.setPreferredSize(new Dimension(200,250));
		panel.setLayout(new GridLayout(4, 2));
		addAgendas(CTLEngine.getAllAgendas());
		pAgenda.add(pListeAgenda);
	}
	class ActionListAgendaFrame implements ActionListener{
		 
        public void actionPerformed(ActionEvent e) {
        	CalendarInterface.showUpdateAgendaView();
        }              
	}
	
	class ActionSelectedAgenda implements ActionListener{
		CEMAgenda oAgenda;
		public ActionSelectedAgenda(CEMAgenda p_oAgenda) {
			oAgenda = p_oAgenda;
		}
		
        public void actionPerformed(ActionEvent e) {
        	if(oAgenda.isActivated()) {
        		oAgenda.setActivated(false);
        	}
        	else {
        		oAgenda.setActivated(true);
        	}
        	CalendarInterface.updatePanel(CalendarInterface.getSideCalDate());
        }             
	}
	
	class ActionCalendar implements PropertyChangeListener{
		 
        public void actionPerformed(ActionEvent e) {
        	
        }            

		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			 if ("calendar".equals(evt.getPropertyName())) {
	                Calendar cal = (Calendar) evt.getNewValue();
	                CalendarInterface.updatePanel(getFormattedCalendar(cal));
	            }
		}
	}
	
	public Calendar getSideCalDate(){
		 Calendar cal = (Calendar) jCalendar_.getCalendar();
		 Calendar cal2 = getFormattedCalendar(cal);
		 return cal2;
	}
	
	private Calendar getFormattedCalendar(Calendar p_oCalendar) {
        Calendar cal = Calendar.getInstance(Locale.FRANCE);
        cal.set(Calendar.YEAR, p_oCalendar.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, p_oCalendar.get(Calendar.MONTH));
        cal.set(Calendar.WEEK_OF_YEAR, p_oCalendar.get(Calendar.WEEK_OF_YEAR));
        cal.set(Calendar.DAY_OF_YEAR, p_oCalendar.get(Calendar.DAY_OF_YEAR));
		return cal;
	}

	public void setSideCalDate(Calendar cal){
		jCalendar_.setCalendar(cal);
	}
	
}
